package com.itheima.demo.UDPdemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Service {//UDP接受数据,接收端不知道发送端什么时候结束发送,所以采用死循环接受
    public static void main(String[] args) throws IOException {
        //码头,接受码头的端口为1000
        DatagramSocket socket = new DatagramSocket(1000);
        byte[] bytes =new byte[1024];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
        String s;
        while(true){
            socket.receive(packet);//解析包裹,将礼物放到新包裹中
            if((s= new String(packet.getData(),0,packet.getLength())).equals("886")){
                break;
            }else{
                System.out.println(s);
            }
        }
        //释放资源
        socket.close();
    }
}
